﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace TibcoQueueMonitor.Entities
{
    /// <summary>
    /// Represents a TIBCO EMS client connection.
    /// </summary>
    [Serializable]
    public class TibcoConnection
    {
        #region Constants
        private const string RegExUptime = @"^\d+:\d\d:\d\d";
        private const string RegExUptimeSec = @"^\d+\.\d+";

        #endregion //Constants

        #region Private Fields
        private string _connectionId;
        private string _clientType;
        private string _host;
        private string _user;
        private string _sessionNumber;
        private bool _sslEnabled;
        private bool _faultTolerant;
        private TimeSpan _uptime;

        #endregion //Private Fields

        #region Public Properties
        /// <summary>
        /// Gets or sets the connection ID.
        /// </summary>
        /// <value>The connection ID.</value>
        public string ConnectionID
        {
            get { return _connectionId; }
            set { _connectionId = value; }
        }

        /// <summary>
        /// Gets or sets the type of the client.
        /// </summary>
        /// <value>The type of the client.</value>
        public string ClientType
        {
            get { return _clientType; }
            set { _clientType = value; }
        }

        /// <summary>
        /// Gets or sets the host.
        /// </summary>
        /// <value>The host.</value>
        public string Host
        {
            get { return _host; }
            set { _host = value; }
        }

        /// <summary>
        /// Gets or sets the user.
        /// </summary>
        /// <value>The user.</value>
        public string User
        {
            get { return _user; }
            set { _user = value; }
        }

        /// <summary>
        /// Gets or sets the session number.
        /// </summary>
        /// <value>The session number.</value>
        public string SessionNumber
        {
            get { return _sessionNumber; }
            set { _sessionNumber = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the connection is SSL
        /// enabled.
        /// </summary>
        /// <value><c>true</c> if SSL enabled; otherwise, <c>false</c>.</value>
        public bool SSLEnabled
        {
            get { return _sslEnabled; }
            set { _sslEnabled = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the connection is fault
        /// tolerant.
        /// </summary>
        /// <value><c>true</c> if connection is fault tolerant; otherwise,
        /// <c>false</c>.</value>
        public bool FaultTolerant
        {
            get { return _faultTolerant; }
            set { _faultTolerant = value; }
        }

        /// <summary>
        /// Gets or sets the uptime.
        /// </summary>
        /// <value>The uptime.</value>
        public TimeSpan Uptime
        {
            get { return _uptime; }
            set { _uptime = value; }
        }

        #endregion //Public Properties

        #region Public Methods
        /// <summary>
        /// Sets the uptime.
        /// </summary>
        /// <param name="uptime">The uptime.</param>
        public void SetUptime(string uptime)
        {
            if (Regex.IsMatch(uptime, RegExUptime))
            {
                // Uptime is hh:mm:ss format
                int hours = int.Parse(uptime.Substring(0, uptime.IndexOf(':')));
                _uptime = TimeSpan.FromHours(hours);
                _uptime = _uptime.Add(TimeSpan.Parse(String.Concat("00:", uptime.Substring(uptime.IndexOf(':') + 1))));
            }
            else if (Regex.IsMatch(uptime, RegExUptimeSec))
            {
                // Uptime is ss.millisecond format
                _uptime = TimeSpan.FromSeconds(double.Parse(uptime));
            }
            else
            {
                _uptime = new TimeSpan();
            }
        }

        #endregion //Public Methods
    }
}
